package com.lmf.cloud.resp;

import com.lmf.cloud.em.ReturnCodeEnum;
import lombok.Data;
import lombok.experimental.Accessors;

/**
 * @Description 全局统一响应封装类
 * <p>
 * Copyright @2022 Shanghai Mise Co. Ltd.
 * All right reserved.
 * <p>
 * @Author LiuMingFu
 * @Date 2024/7/18 19:07
 */
@Data
@Accessors(chain = true)
public class ResultData<T> {
    private String code;
    private String message;
    private boolean success;
    private T data;
    private long timestamp;//调用方法的时间戳

    public ResultData() {
        this.timestamp = System.currentTimeMillis();
    }


    /**
     * 成功响应
     *
     * @param data 数据
     * @return
     * @author LiuMingFu
     * @date 2024/8/1
     */
    public static <T> ResultData<T> success(T data) {
        ResultData<T> resultData = new ResultData<>();
        resultData.setCode(ReturnCodeEnum.RC200.getCode());
        resultData.setMessage(ReturnCodeEnum.RC200.getMessage());
        resultData.setSuccess(true);
        resultData.setData(data);
        return resultData;
    }


    /**
     * 失败响应
     *
     * @param code    失败响应码
     * @param message 失败响应消息
     * @return
     * @author LiuMingFu
     * @date 2024/8/1
     */
    public static <T> ResultData<T> fail(String code, String message) {
        ResultData<T> resultData = new ResultData<>();
        resultData.setCode(code);
        resultData.setMessage(message);
        resultData.setSuccess(false);
        return resultData;
    }


    /**
     * 失败响应
     *
     * @param returnCodeEnum 失败响应枚举类
     * @return
     * @author LiuMingFu
     * @date 2024/8/1
     */
    public static <T> ResultData<T> fail(ReturnCodeEnum returnCodeEnum) {
        ResultData<T> resultData = new ResultData<>();
        resultData.setCode(returnCodeEnum.getCode());
        resultData.setMessage(returnCodeEnum.getMessage());
        resultData.setSuccess(false);
        return resultData;
    }
}
